package net.sf.rose.business.action.define;

import javax.servlet.http.HttpServletRequest;

import net.sf.rose.business.bean.control.judge.JudgeBean;
import net.sf.rose.business.bean.control.judge.JudgeStatement;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 功能：
 * 
 * @author fengjian
 * @since 2014/06/04
 */
@Controller
@RequestMapping("/define/judge")
public class JudgeController {

	private static String LIST_PAGE = "define/judge/list";
	private static String VIEW_PAGE = "define/judge/edit";
	private static String BEAN_TEMP = "DEFINE_JUDGE_BEAN_TEMP";
	private static String BEAN_TEMP_NAME = "DEFINE_JUDGE_BEAN_TEMP_NAME";
	public static String JUDGE_NAME_KEY = "judgeName";
	public static String JS_BACK_FUNCTION_NAME = "jsbackfunctionname";

	@RequestMapping("/list.html")
	public ModelAndView list(HttpServletRequest request) {
		JudgeBean bean = new JudgeBean();
		String jsbackfn = request.getParameter(JS_BACK_FUNCTION_NAME);
		if (jsbackfn == null) {
			Object obj = request.getSession().getAttribute(JS_BACK_FUNCTION_NAME);
			if (obj != null) {
				jsbackfn = obj.toString();
			}
		} else {
			request.getSession().setAttribute(JS_BACK_FUNCTION_NAME, jsbackfn);
		}
		Object obj = request.getSession().getAttribute(JUDGE_NAME_KEY);
		if (obj != null) {
			String key = obj.toString();
			obj = request.getSession().getAttribute(key);
			if (obj != null) {
				bean = (JudgeBean) obj;
			}
			request.getSession().setAttribute(BEAN_TEMP_NAME, key);
		}
		request.getSession().setAttribute(BEAN_TEMP, bean);
		ModelAndView mav = new ModelAndView(LIST_PAGE);
		mav.addObject("list", bean);
		mav.addObject("jsbackfn", jsbackfn);
		return mav;
	}

	@RequestMapping("/add.html")
	public ModelAndView add(HttpServletRequest request) {
		JudgeStatement statement = new JudgeStatement();
		ModelAndView mav = new ModelAndView(VIEW_PAGE);
		mav.addObject("bean", statement);
		return mav;
	}

	@RequestMapping("/edit.html")
	public ModelAndView edit(HttpServletRequest request, int id) {
		JudgeStatement statement = null;
		Object obj = request.getSession().getAttribute(BEAN_TEMP);
		if (obj != null) {
			JudgeBean bean = (JudgeBean) obj;
			statement = bean.getJudge(id);
		} else {
			statement = new JudgeStatement();
		}
		ModelAndView mav = new ModelAndView(VIEW_PAGE);
		mav.addObject("bean", statement);
		return mav;
	}
	
	@RequestMapping("/delete.html")
	public ModelAndView delete(HttpServletRequest request, int id) {
		Object obj = request.getSession().getAttribute(BEAN_TEMP);
		if (obj != null) {
			JudgeBean bean = (JudgeBean) obj;
			bean.removeJudge(id);
		}
		ModelAndView mav = null;
		mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "操作成功！");
		mav.addObject("callbackType", true);
		return mav;
	}
	
	/**
	 * 提交操作
	 */
	@RequestMapping("/save.html")
	public ModelAndView save(HttpServletRequest request,JudgeStatement statement) {
		Object obj = request.getSession().getAttribute(BEAN_TEMP);
		if (obj != null) {
			JudgeBean bean = (JudgeBean) obj;
			bean.addJudge(statement);
		}
		ModelAndView mav = null;
		mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "操作成功！");
		mav.addObject("callbackType", true);
		return mav;
	}
}
